Building a Tip of the Day Application (Part 3): Submitting Tips to the Web Service
Table of Contents
Displaying a Tip from the Search Results
When a user gets a list of results from the keyword search, you want him to be able to view the result by clicking the tip’s name.
-
Write code to load a tip when a user clicks a search result.
- Select the
results_lsList component instance on the searchresult screen. - Open the Actions panel and add the following code onto the
results_lsinstance:this._parent._parent._parent.dataHolder_dh.tipdate_str = this.selectedItem.data;
Note: The data field of the List component holds preformatted date information from the web service. You can bind the data directly to the DataHolder component instead of having to call the custom
dateFormat()function in Macromedia Flash to format the date for you. - Select the
-
Add a behavior to load the tip screen when a user selects a tip from the List instance.
- Select the
results_lsinstance on the Stage. - Click the Add Behavior button in the Behaviors panel and select Screens > Go to Slide from the contextual menu.
- Select the tip screen and click OK to close the dialog box.
- Make sure the Go to Slide event is set to change in the pop-up menu.
- Check that the code you added to set the
tipdate_strvariable in theDataHolder_dhinstance comes before the code that loads a new screen but after theon change()handler, like this:on (change) this._parent._parent._parent.dataHolder_dh.tipdate_str = this.selectedItem.data; // GoTo Screen behavior
- Select the
-
Select Control > Test Movie to test the SWF file.
Now you can perform a keyword search, click a result in the List component instance, and be redirected to the home page with the new tip loading in.
-
Add code to check the number of values being returned by the XML packet and display a logical error informing the user that no results were found.
- Click the presentation screen and click the
getTipsByKeyword_wscinstance. -
Replace the existing code on the instance with the code below:
on (result) { var dh = this._parent.dataHolder_dh; XML.prototype.ignoreWhite = true; dh.keywordresults = new XML(this.results); if (dh.keywordresults.firstChild.childNodes.length == 0) { dh.keywordresults = new XML("<results><tip><TipTitle>No search results found.</TipTitle><TipDateTime /><ProductIcon /></tip></results>"); } }
The above code first creates a shortcut variable
(dh)to thedataHolder_dhinstance. This simplifies your subsequent code; you don't have to type out the full path to the component each time. Next, the if statement checks to see if the web service passes back any results. It does this by checking the length of thechildNodesarray, a Flash XML object Flash creates from the XML data returned by the web service. If the number of results equals zero, you create your own XML packet which will be bound to the List component. It will say No search results found rather than displaying an empty List with no results. This way, the user knows that the search successfully completed, but they just didn’t get any matching results. - Click the presentation screen and click the
-
Disable the List component if no search results were returned.
Although it seems like everything is possibly working, there is still one strange quirk in the application. If no search results are returned, the app displays an error message, but a user can still click on the No results were found message and generate an error when Flash tries to connect to the web service. This happens because the data field for the error message didn’t contain a date.
There are three possible solutions to this problem:
- Make up a date to pass in the error message. (This is not a very good solution because the date wouldn’t be meaningful at all.)
- Check that the date is valid before continuing with the
on (change)for the List component. -
Disable the List component if no results were found to prevent the user from clicking anything.
Both 2 and 3 are acceptable solutions, but because option 3 makes for a more interesting article and neither of the options are more than a couple lines of code, you’re stuck reading about the last option!
If you want to disable a List component (gray it out) or toggle whether a List component is selectable, modify the code on the
getTipsByKeyword_wsc.- Select
getTipsByKeyword_wscon the Stage. -
Open the Actions panel and change the
ifstatement to the following snippet:if (dh.keywordresults.firstChild.childNodes.length == 0) { dh.keywordresults = new XML("<results><tip><TipTitle>No search results found.</TipTitle><TipDateTime /><ProductIcon /></tip></results>"); this._parent.searchform.searchresult.results_ls.selectable = false; } else { this._parent.searchform.searchresult.results_ls.selectable = true; }
You’ve set the List component’s ability to be selectable based on the condition in the
ifstatement. You've added anelsecondition as well. This prevents a user from being able to select the error message.
Figure 7.
results_lsList component instance with enabled set to false
Note: Even though users cannot select the error
message, they can still mouse over it. The error message will highlight,
however, it won’t react to mouse clicks. If you find this behavior
confusing, you can replace selectable with enabled.
This makes the List component gray out.